<script setup>
import { RouterView } from 'vue-router'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const isLoggedIn = ref(false)
const userInfo = ref(null)

// 初始化时检查用户登录状态
const checkLoginStatus = () => {
  const user = localStorage.getItem('user')
  if (user) {
    const parsedUser = JSON.parse(user)
    isLoggedIn.value = parsedUser.isLoggedIn
    userInfo.value = parsedUser
  } else {
    isLoggedIn.value = false
    userInfo.value = null
  }
}

// 登出功能
const handleLogout = () => {
  localStorage.removeItem('user')
  isLoggedIn.value = false
  userInfo.value = null
  router.push('/login')
}

onMounted(() => {
  checkLoginStatus()
})
</script>

<template>
  <div class="app-container">
    <!-- 导航栏 - 透明浮动导航栏 -->
    <nav v-if="isLoggedIn" class="navbar">
      <div class="container">
        <div class="navbar-content">
          <div class="navbar-brand">
            <router-link to="/home">Vue Frontend</router-link>
          </div>
          <div class="navbar-menu">
            <router-link to="/home">首页</router-link>
            <router-link to="/about">关于</router-link>
          </div>
          <div class="navbar-user">
            <span class="welcome-text">欢迎, {{ userInfo?.username }}</span>
            <button class="logout-btn" @click="handleLogout">退出登录</button>
          </div>
        </div>
      </div>
    </nav>
    
    <!-- 路由视图 -->
    <RouterView />
  </div>
</template>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  line-height: 1.6;
  color: #333;
  background-color: #f5f5f5;
  overflow-x: hidden;
}

.app-container {
  width: 100%;
  min-height: 100vh;
  position: relative;
}

.news-home {
  min-height: 100vh;
  background: #fff;
}

a {
  text-decoration: none;
  color: inherit;
  transition: color 0.3s ease;
}

ul, ol {
  list-style: none;
}

img {
  max-width: 100%;
  height: auto;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 导航栏样式 */
.navbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background-color: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  padding: 20px 0;
  transition: background-color 0.3s ease;
}

.navbar:hover {
  background-color: rgba(255, 255, 255, 0.2);
}

.navbar-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.navbar-brand {
  font-size: 24px;
  font-weight: bold;
  color: white;
}

.navbar-menu {
  display: flex;
  gap: 30px;
}

.navbar-menu a {
  color: white;
  font-size: 16px;
  font-weight: 500;
}

.navbar-menu a:hover {
  color: #f0f0f0;
  text-decoration: underline;
}

.navbar-user {
  display: flex;
  align-items: center;
  gap: 20px;
  color: white;
}

.welcome-text {
  font-size: 16px;
}

.logout-btn {
  padding: 8px 16px;
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  background-color: rgba(255, 255, 255, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .navbar-content {
    flex-direction: column;
    gap: 15px;
  }
  
  .navbar-menu {
    gap: 20px;
  }
  
  .navbar-user {
    gap: 15px;
  }
}
</style>
